package uy.edu.um.ltic.client.communication;

import java.rmi.NotBoundException; 
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.sql.Timestamp;
import java.util.ArrayList;

import uy.edu.um.ltic.commons.EmployeeVO;
import uy.edu.um.ltic.commons.ExceptionComm;
import uy.edu.um.ltic.commons.MessageRemoteMgt;
import uy.edu.um.ltic.commons.MessageVO;
import uy.edu.um.ltic.exception.DataBaseException;

public class MessageRemoteClientMgr implements MessageRemoteClientMgt{

	private static MessageRemoteClientMgr instance = null;

	private MessageRemoteClientMgr() {
	}

	public static MessageRemoteClientMgr getInstance() {
		if (instance == null) {
			instance = new MessageRemoteClientMgr();
		}
		return instance;
	}
	
	@Override
	public void addMessage(MessageVO oMessage) throws ExceptionComm, DataBaseException {

		String sObjectService = "MessageMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			MessageRemoteMgt oMessageRemoteMgt = (MessageRemoteMgt) 
					oRegitry.lookup(sObjectService); 
			oMessageRemoteMgt.addMessage(oMessage);
		
		} catch (RemoteException | NotBoundException e) {
				throw new ExceptionComm(e);	
		
		} 
	}

	@Override
	public ArrayList<MessageVO> getMessagesBetweenTwo(EmployeeVO sender, EmployeeVO receiver)  throws ExceptionComm, DataBaseException {
		String sObjectService = "MessageMgt"; 
		Registry oRegitry;
		MessageRemoteMgt oMessageRemoteMgt = null;
		ArrayList<MessageVO> oList = new ArrayList<MessageVO>();
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			oMessageRemoteMgt = (MessageRemoteMgt) 
					oRegitry.lookup(sObjectService);
			oList = oMessageRemoteMgt.getMessagesBetweenTwo(sender, receiver);
		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		} 
		return oList;
	}

	@Override
	public ArrayList<MessageVO> getConferenceMessages(int idConference) throws ExceptionComm, DataBaseException {
		String sObjectService = "MessageMgt"; 
		Registry oRegitry;
		MessageRemoteMgt oMessageRemoteMgt = null;
		ArrayList<MessageVO> oListMess = new ArrayList<MessageVO>();
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			oMessageRemoteMgt = (MessageRemoteMgt) 
					oRegitry.lookup(sObjectService);
			oListMess = oMessageRemoteMgt.getConferenceMessages(idConference);
		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		} 
		return oListMess;
	}

	@Override
	public ArrayList<EmployeeVO> getLastEmployees(int nEmployee) throws ExceptionComm, DataBaseException {
		String sObjectService = "MessageMgt"; 
		Registry oRegitry;
		MessageRemoteMgt oMessageRemoteMgt = null;
		ArrayList<EmployeeVO> oListEmpl = new ArrayList<EmployeeVO>();
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			oMessageRemoteMgt = (MessageRemoteMgt) 
					oRegitry.lookup(sObjectService);
			oListEmpl = oMessageRemoteMgt.getLastEmployees(nEmployee);
		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		} 
		return oListEmpl;
	}

	@Override
	public ArrayList<MessageVO> getMessagesWithTheWord(String word) throws ExceptionComm, DataBaseException {
		String sObjectService = "MessageMgt"; 
		Registry oRegitry;
		MessageRemoteMgt oMessageRemoteMgt = null;
		ArrayList<MessageVO> oListMess = new ArrayList<MessageVO>();
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			oMessageRemoteMgt = (MessageRemoteMgt) 
					oRegitry.lookup(sObjectService);
			oListMess = oMessageRemoteMgt.getMessagesWithTheWord(word);
		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		} 
		return oListMess;
	}

	@Override
	public int getAmountOfWordsWritten(int nEmployee, Timestamp date1,
			Timestamp date2) throws ExceptionComm, DataBaseException {
		String sObjectService = "MessageMgt"; 
		Registry oRegitry;
		MessageRemoteMgt oMessageRemoteMgt = null;
		int res = 0;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			oMessageRemoteMgt = (MessageRemoteMgt) 
					oRegitry.lookup(sObjectService);
			res = oMessageRemoteMgt.getAmountOfWordsWritten(nEmployee, date1, date2);
		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		} 
		return res;
	}

	@Override
	public ArrayList<Integer> getFilesFromOneEmployeeToAnother(int sender,
			int receiver) throws ExceptionComm, DataBaseException {
		String sObjectService = "MessageMgt"; 
		Registry oRegitry;
		MessageRemoteMgt oMessageRemoteMgt = null;
		ArrayList<Integer> oListFile = new ArrayList<Integer>();
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			oMessageRemoteMgt = (MessageRemoteMgt) 
					oRegitry.lookup(sObjectService);
			oListFile = oMessageRemoteMgt.getFilesFromOneEmployeeToAnother(sender, receiver);
		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		} 
		return oListFile;
	}

	@Override
	public String getNameOfFile(int id) throws ExceptionComm, DataBaseException {
		String sObjectService = "MessageMgt"; 
		Registry oRegitry;
		MessageRemoteMgt oMessageRemoteMgt = null;
		String oFileName = new String();
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			oMessageRemoteMgt = (MessageRemoteMgt) 
					oRegitry.lookup(sObjectService);
			oFileName = oMessageRemoteMgt.getNameOfFile(id);
		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		} 
		return oFileName;
	}

	@Override
	public byte[] getFile(int id) throws ExceptionComm, DataBaseException {
		String sObjectService = "MessageMgt"; 
		Registry oRegitry;
		MessageRemoteMgt oMessageRemoteMgt = null;
		byte[] oFile = null;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			oMessageRemoteMgt = (MessageRemoteMgt) 
					oRegitry.lookup(sObjectService);
			oFile = oMessageRemoteMgt.getFile(id);
		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		} 
		return oFile;
	}

	

	
	

}
